package com.health;

import com.health.pojo.Product;
import org.elasticsearch.search.sort.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * lasticsearchRestTemplate实现排序和分页
 */
@SpringBootTest
public class ElasticsearchRestTemplatePage {

    @Resource
    private ElasticsearchRestTemplate restTemplate;

    /**
     * '分页
     */
    @Test
    void test3(){
        //分页参数
        int page=3;
        int size=5;

        //创建分页请求,当前页-1,后端分页从0开始
        PageRequest pageRequest = PageRequest.of(page - 1, size);


        //创建query请求
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withPageable(pageRequest).build();

        //查询
        SearchHits<Product> searchHits = restTemplate.search(query, Product.class);

        for (SearchHit<Product> searchHit : searchHits) {
            System.out.println(searchHit.getContent());
        }
    }

    /**
     * 排序sort
     * 动态-默认根据分数排序
     */
    @Test
    void test2(){
        //排序字段及排序方式(升序/降序)
        String sortField="price";
        String sortType="asc";

        //默认分数排序-此处必须使用父类接口接收参数,否则无法添加排序及方式
        SortBuilder sortBuilder= SortBuilders.scoreSort();
        //判断前端传递的排序字段及方式
        if (StringUtils.hasText(sortField) && StringUtils.hasText(sortType)){
            sortBuilder=SortBuilders.fieldSort(sortField);
            //判断排序方式
            if (sortType.equals("asc")){
                sortBuilder.order(SortOrder.ASC);
            }else if (sortType.equals("desc")){
                sortBuilder.order(SortOrder.DESC);
            }
        }

        //query请求创建
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withSort(sortBuilder).build();

        //查询
        SearchHits<Product> searchHits = restTemplate.search(searchQuery, Product.class);

        for (SearchHit<Product> searchHit : searchHits) {
            System.out.println(searchHit.getContent());
        }
    }

    /**
     * 排序sort
     */
    @Test
    void test1(){
        //根据price升序排列asc

        //创建sort
        FieldSortBuilder sortBuilder = SortBuilders.fieldSort("price").order(SortOrder.ASC);


        //创建query请求参数
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withSort(sortBuilder).build();

        //查询
        SearchHits<Product> searchHits = restTemplate.search(searchQuery, Product.class);
        //遍历
        for (SearchHit<Product> searchHit : searchHits) {
            System.out.println(searchHit.getContent());
        }

    }
}